/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.systemcar.controller;

import br.com.systemcar.dao.UsuarioDao;
import br.com.systemcar.model.Usuario;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author aluno
 */
@ManagedBean
public class LoginCtrl extends Mensagens{
    
    private String login;
    private String Senha;
    
    @Inject
    UsuarioDao userDao;
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return Senha;
    }

    public void setSenha(String Senha) {
        this.Senha = Senha;
    }
    
    
    
    public void logar() throws IOException{
         // FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Bem Vindo", null));
        Usuario user = new Usuario();
        try{
             user=userDao.findUser(login);
        }catch(Exception e){
             msgErro("Usuário ou senha inválido ");
             return;
        }
       
        String senha="";
        
        
        
        try {
             senha = util.Descriptografa.criptografar(this.Senha);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(LoginCtrl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        if(!senha.equals(user.getUsrSenha())){
            msgErro("Usuário ou senha inválido ");
            return;
            
            
        }
       
        
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("logado", true);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user);
        FacesContext.getCurrentInstance().getExternalContext().redirect("/faces/faces/Admin/index.xhtml");
      
//        return "sussess";
        
       
    }
    
    public void verificaLogin() throws IOException{
          HttpServletResponse rs =  (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        
        boolean logado =false;
        if(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("logado")!=null){
            logado=(Boolean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("logado");
        }
        
        if(!logado){
          
       
       rs.sendError(HttpServletResponse.SC_FORBIDDEN);
        }
        
            FacesContext.getCurrentInstance().renderResponse();
        
    }
    
    public String msUsuario(){
        Usuario user =(Usuario) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
       
        if (user!=null){
        return user.getUsrNome();
        }
        return "";
    }
    
    public void logOut() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("/faces/faces/login.xhtml");
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    }
    
    public Usuario getUsuario(){
    
        return (Usuario)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
    }
    
}
